package com.lonty.intelligence.service;

import com.lonty.intelligence.mapper.LeavingMessageMapper;
import com.lonty.intelligence.model.LeavingMessage;
import com.lonty.intelligence.request.LeavingMessageAndUser;
import com.lonty.intelligence.util.UUIDUtil;
import com.lonty.intelligence.vo.LeavingMessageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/6/11 13:12
 * @Version 1.0
 */
@Service
public class LeavingMessageService {

    @Resource
    private LeavingMessageMapper leavingMessageMapper;

    /**
     * 保存留言
     * @param leavingMessageVo
     * @return
     */
    public boolean saveLeavingMessage(LeavingMessageVo leavingMessageVo){
        LeavingMessage leavingMessage = new LeavingMessage();
        BeanUtils.copyProperties(leavingMessageVo, leavingMessage);
        leavingMessage.setId(UUIDUtil.randomID());
        leavingMessage.setCreateTime(LocalDateTime.now());
        return leavingMessageMapper.saveLeavingMessage(leavingMessage) > 0;
    }

    /**
     * 根据ID获取留言
     * @param id
     * @return
     */
    public LeavingMessageAndUser getById(String id){
        return leavingMessageMapper.getById(id);
    }

    /**
     * 根据用户ID获取留言
     * @param userInfoId
     * @return
     */
    public List<LeavingMessage> getByUserId(String userInfoId){
        return leavingMessageMapper.getByUserId(userInfoId);
    }

    /**
     * 获取社区所有留言
     * @param communityId
     * @return
     */
    public List<LeavingMessageAndUser> getLeavingMessageAndUser(String communityId){
        return leavingMessageMapper.getLeavingMessageAndUser(communityId);
    }

}
